package com.sys.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sys.mapper.PermissionMapper;
import com.sys.mapper.RolePermissionMapper;
import com.sys.pojo.Permission;
import com.sys.pojo.Role;
import com.sys.pojo.RolePermission;
import com.sys.service.RolePermissionService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author ZJF
 * @since 2021-04-01
 */
@Service
public class RolePermissionServiceImpl extends ServiceImpl<RolePermissionMapper, RolePermission> implements RolePermissionService {

    @Resource
    private RolePermissionMapper rolePermissionMapper;

    @Resource
    private PermissionMapper permissionMapper;

    @Override
    public List<Permission> getRolePermission(List<Role> roles) {

        if (roles.size() != 0) {
            List<Permission> permissions = new ArrayList<>();
            for (Role role : roles) {
                LambdaQueryWrapper<RolePermission> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(RolePermission::getRoleId, role.getId());
                List<RolePermission> rolePermissions = rolePermissionMapper.selectList(queryWrapper);
                for (RolePermission rolePermission : rolePermissions) {
                    permissions.add(permissionMapper.selectById(rolePermission.getPermissionId()));
                }
            }
            return permissions;
        } else {
            return null;
        }
    }

    @Override
    public List<Permission> getRolePermissionPerms(List<Role> roles) {

        if (roles.size() != 0) {
            List<Permission> permissions = new ArrayList<>();
            for (Role role : roles) {
                LambdaQueryWrapper<RolePermission> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(RolePermission::getRoleId, role.getId());
                List<RolePermission> rolePermissions = rolePermissionMapper.selectList(queryWrapper);
                for (RolePermission rolePermission : rolePermissions) {
                    Permission permission = permissionMapper.selectById(rolePermission.getPermissionId());
                    if (permission.getStatus().equals("2")) {
                        permissions.add(permission);
                    }
                }
            }
            return permissions;
        } else {
            return null;
        }
    }


    @Override
    public List<RolePermission> getOneRolePermission(Integer rId) {
        LambdaQueryWrapper<RolePermission> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RolePermission::getRoleId, rId);
        return rolePermissionMapper.selectList(queryWrapper);

    }
}
